<template>
  <div class="xy-date-picker">
    <div
      @click="dateIndex = 1"
      :class="dateIndex == 1 ? 'date-item-active date-item' : 'date-item'"
    >
      日
    </div>
    <div
      @click="dateIndex = 2"
      :class="dateIndex == 2 ? 'date-item-active date-item' : 'date-item'"
    >
      月
    </div>
    <div
      @click="dateIndex = 3"
      :class="dateIndex == 3 ? 'date-item-active date-item' : 'date-item'"
    >
      年
    </div>
    <div
      @click="dateIndex = 4"
      :class="dateIndex == 4 ? 'date-range' : 'date-range'"
    >
      <el-date-picker
        v-model="timeValue"
        type="date"
        placeholder="选择日期范围"
        align="right"
        :picker-options="pickerOptions"
        value-format="yyyy-MM-dd"
        size="mini"
      >
      </el-date-picker>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      timeValue: "",
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "近七天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
          {
            text: "近30天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", date);
            },
          },
          {
            text: "本月",
            onClick(picker) {
              const date = new Date();
              date.setDate(1); // 设置为本月第一天
              picker.$emit("pick", date);
            },
          },
          {
            text: "上个月",
            onClick(picker) {
              const now = new Date();
              // 创建一个新的日期对象，设置为上个月的第一天
              const firstDayOfLastMonth = new Date(
                now.getFullYear(),
                now.getMonth() - 1,
                1
              );
              picker.$emit("pick", firstDayOfLastMonth);
            },
          },
        ],
      },
      dateIndex: 1,
    };
  },
};
</script>

<style scoped lang="scss">
.xy-date-picker {
  display: flex;
  align-items: center;
  background: rgba(0, 102, 255, 0.08);
  padding: 5px 8px;
  border-radius: 12px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(0, 102, 255, 0.15);

  .date-item {
    width: 40px;
    height: 26px;
    line-height: 26px;
    display: flex;
    justify-content: center;
    cursor: pointer;
    border-radius: 10px;
    margin-right: 5px;
    background-color: transparent;
    color: #0066ff;
    font-size: 12px;
  }

  .date-item-active {
    background-color: #0066ff;
    cursor: pointer;
    border-radius: 10px;
    margin-right: 10px;
    color: #fff;
  }

  .date-range {
    width: 120px;
    cursor: pointer;
    border-radius: 10px;
    margin-right: 0;
    background-color: #0066ff;
    position: relative;
  }
}
</style>

<style lang="scss">
.xy-date-picker {
  .el-date-editor.el-input {
    width: 100%;
    border: none;
    background: transparent;
    margin: 0;
    padding: 0;
    height: 26px;
    line-height: 26px;
    .el-input__inner {
      border: none;
      background: transparent;
      padding: 0;
      padding-left: 26px;
      height: 26px;
      line-height:26px;
      color: #fff;
      cursor: pointer;
      &::placeholder {
        color: #fff; // 设置 placeholder 颜色
      }
    }
    .el-input__prefix,
    .el-input__suffix{
        color: #fff;
    }
  }
}
</style>
